package com.tianfeng.huayi.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.http.ResponseEntity;

import java.util.*;
import java.util.ArrayList;

public class AlipayTradePrecreate {

    // 生成支付二维码
    public static ResponseEntity<Map<Object, Object>> aliPayQrCode(String subject, String flowingWater, String price) throws AlipayApiException {
        Map<Object, Object> response = new HashMap<>();
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

        // 设置订单标题
        model.setSubject(subject);
        // 设置二维码超时时间
        model.setQrCodeTimeoutExpress("5m");

        // 设置商户订单号
        model.setOutTradeNo(flowingWater);
        // 设置订单总金额
        model.setTotalAmount(price);
        // 设置卖家支付宝用户ID
        model.setSellerId("2088721023668995");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePrecreateResponse precreateResponse = alipayClient.execute(request);
        System.out.println(precreateResponse.getBody());

        if (precreateResponse.isSuccess()) {
            System.out.println("调用成功");
            System.out.println(precreateResponse);
            response.put("code", 200);
            response.put("msg", "调用成功");
            response.put("data", precreateResponse.getQrCode());
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
            response.put("code", 400);
            response.put("msg", "调用失败");
        }
        return ResponseEntity.ok(response);
    }

    // 查询是否已经扫码支付成功
    public static ResponseEntity<Map<Object, Object>> confirmAlipay(String flowingWater) throws AlipayApiException {
        Map<Object, Object> response = new HashMap<>();
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();

        // 设置订单支付时传入的商户订单号
        model.setOutTradeNo(flowingWater);

        // 设置查询选项
        List<String> queryOptions = new ArrayList<String>();
        queryOptions.add("trade_settle_info");
        model.setQueryOptions(queryOptions);

        request.setBizModel(model);
        AlipayTradeQueryResponse tradeQueryResponse = alipayClient.execute(request);
        System.out.println(tradeQueryResponse.getBody());

        if (tradeQueryResponse.isSuccess()) {
            System.out.println("调用成功");
            System.out.println(tradeQueryResponse);
            response.put("code", 200);
            response.put("msg", "调用成功");
            response.put("data", tradeQueryResponse.getTradeStatus());
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
            response.put("code", 400);
            response.put("msg", "调用失败");
        }
        return ResponseEntity.ok(response);
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC6jR+Bi3kF9lmErSmc3pUagF0qnGKZFAsXax2Vpy5+5rFbR+ODG+fn7gZMZ7Nvw59lIBjwAysN9aNCj6X5EZeAM0vI7V3aezCBQhf3ACO4FLJ2XN+X73yUvzqAnlrmAichPixf4KhYb7d1Mv+AGNHWwD8uz4Ci+ANphh7gZA+yA3MwuH76yq5jUTxIgjGW0npF9Mx0I/D0gup2SC+KTyX5PjkOWBHdr7EQZFiupnLecSKPCRGQbajiVs98GxfoCGUdfJdzUZW62kEm0D5Hl+aqsopCRlPctrKfSzZ+Vs869Vz4fim21BG3LMQinZe9LZs83VOWZX6rjlEbRM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BJUSJYAd+Vj0L3C0HXdhh7Ner3JB9pPIOSOQe4+7mZFWdRiOUS81DZ2QRmYDLREM+hy8pzhxXtMnihUAHD7mcLQ3LMG2sc/M7Lk+4w6qJg1xFXfVJUoh6X18Lu7ffxQLcA9Ap/RDh0R8oXi2c1oXyOBis/JxUL+A==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApQdXeiXHlN17W9z1kY+k2HmuFZQVWO5UfngKgZf660MemudtWecog4aAl7h9lRHoZveqpuYK7LxLBbK48odDdnNOaNiCvqsUnZS614RkdpEiIOnQ7e2I5N7MmPC9svy+R+GkAhW2Ds74GMiV+ybnW+j779MG7tNxGviFTLG4sDks6RZ/HQVkXLBhzyZyWjSzzWQX9/4W5EFf2L4l5vJmogLBxyGLgoV7Fzg0IGp7EXnzycFyGqdMFWfPawXcjCtzjWg9t7Pu0KNvz3mm6JmrHJltaYss7EczRFiXfaNYrJKN+EgY4cXYTqJLQMHiXxNbxvBpgPo8T3Wvj1p/NqVbjQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000132659408");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}